<?php


namespace App\Api\Controller;

use EchoPHP\Facades\Log;
use EchoPHP\Http\Request;

use App\Api\Model\Interview;
use Exception;

class InterviewCallback extends BaseController {

    public function __construct(Request $request) {
        /*
        $postData = $request->getPost();
        $str = var_export($postData, true);
        $content = date("Y-m-d H:i", time()) . ' ' . $request->getUrl();
        $content .= "\r\n" . $str;
        $content .= "\r\n";

        file_put_contents('../../storage/log/web/log.txt', $content, FILE_APPEND);
        */
    }

    /**
     * 面试官提醒回调接口
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function remindInterviewer(Request $request) {
        $interviewModel = new Interview();
        try {
            $postData = $request->getPost();

            $checkResult = $interviewModel->checkToken($postData);
            if ($checkResult != 1) {
                $interviewModel->sendResult($checkResult);
            }

            $remindtype = $postData['remindtype'];    //1:安排面试时提醒 2:面试开始30分前提醒 3:手动触发
            $interviewerid_out = $postData['interviewerid'];

            //$remindtype = 1;
            //$interviewerid_out = 'f00e0be4-c64e-42a3-b3f8-785da6274949';

            $interviewModel->remindInterviewer($interviewerid_out, $remindtype);
            $interviewModel->sendResult(1);
        } catch (Exception $ex) {
            $interviewModel->sendResult(-9, $ex->getMessage());
        }
    }

    /**
     * 求职者提醒回调接口
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function remindJobseeker(Request $request) {
        $interviewModel = new Interview();
        try {
            $postData = $request->getPost();

            $checkResult = $interviewModel->checkToken($postData);
            if ($checkResult != 1) {
                $interviewModel->sendResult($checkResult);
            }

            $remindtype = $postData['remindtype'];    //1:安排面试时提醒 2:面试开始30分前提醒 3:手动触发
            $jobseekerid_out = $postData['jobseekerid'];

            //$remindtype = 1;
            //$jobseekerid_out = '81d588e6-8030-4ea7-92a5-9f784632e1c9';

            $interviewModel->remindJobseeker($jobseekerid_out, $remindtype);
            $interviewModel->sendResult(1);
        } catch (Exception $ex) {
            $interviewModel->sendResult(-9, $ex->getMessage());
        }
    }

    /**
     * 房间信息变更提醒面试官回调接口
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function remindInterviewerRoomChange(Request $request) {
        $interviewModel = new Interview();
        try {
            $interviewModel->sendResult(1);
        } catch (Exception $ex) {
            $interviewModel->sendResult(-9, $ex->getMessage());
        }
    }

    /**
     * 房间信息变更提醒求职者回调接口
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function remindJobseekerRoomChange(Request $request) {
        $interviewModel = new Interview();
        try {
            $interviewModel->sendResult(1);
        } catch (Exception $ex) {
            $interviewModel->sendResult(-9, $ex->getMessage());
        }
    }

    /**
     * 取消面试官提醒回调接口
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function remindCancelInterviewer(Request $request) {
        $interviewModel = new Interview();
        try {
            $postData = $request->getPost();

            $checkResult = $interviewModel->checkToken($postData);
            if ($checkResult != 1) {
                $interviewModel->sendResult($checkResult);
            }

            $interviewerid_out = $postData['interviewerid'];

            //$interviewerid_out = 'f00e0be4-c64e-42a3-b3f8-785da6274949';

            $interviewModel->remindCancelInterviewer($interviewerid_out);
            $interviewModel->sendResult(1);
        } catch (Exception $ex) {
            $interviewModel->sendResult(-9, $ex->getMessage());
        }
    }

    /**
     * 取消求职者提醒回调接口
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function remindCancelJobseeker(Request $request) {
        $interviewModel = new Interview();
        try {
            $postData = $request->getPost();

            $checkResult = $interviewModel->checkToken($postData);
            if ($checkResult != 1) {
                $interviewModel->sendResult($checkResult);
            }

            $jobseekerid_out = $postData['jobseekerid'];

            //$jobseekerid_out = '81d588e6-8030-4ea7-92a5-9f784632e1c9';

            $interviewModel->remindCancelJobseeker($jobseekerid_out);
            $interviewModel->sendResult(1);
        } catch (Exception $ex) {
            $interviewModel->sendResult(-9, $ex->getMessage());
        }
    }

    /**
     * 面试官开始，结束面试回调
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function interviewerWorkStatus(Request $request) {
        $interviewModel = new Interview();
        try {
            $postData = $request->getPost();

            $checkResult = $interviewModel->checkToken($postData);
            if ($checkResult != 1) {
                $interviewModel->sendResult($checkResult);
            }

            $noticetype = $postData['noticetype'];    //1：面试开始  2：面试结束
            $interviewerid_out = $postData['interviewerid'];
            $time = $postData['time'];
            $interviewModel->updateInterviewInfo($interviewerid_out, $noticetype, $time);

            $interviewModel->sendResult(1);
        } catch (Exception $ex) {
            $interviewModel->sendResult(-9, $ex->getMessage());
        }
    }

    /**
     * 求职者开始，结束面试回调
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function jobseekerWorkStatus(Request $request) {
        $interviewModel = new Interview();
        try {
            $postData = $request->getPost();

            $checkResult = $interviewModel->checkToken($postData);
            if ($checkResult != 1) {
                $interviewModel->sendResult($checkResult);
            }

            $noticetype = $postData['noticetype'];    //1：面试开始  2：面试结束
            $jobseekerid_out = $postData['jobseekerid'];
            $time = $postData['time'];

            $interviewModel->updateJobseekerInfo($jobseekerid_out, $noticetype, $time);

            $interviewModel->sendResult(1);
        } catch (Exception $ex) {
            $interviewModel->sendResult(-9, $ex->getMessage());
        }
    }

    /**
     * 求职者评分评语面试回调
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function jobseekerScore(Request $request) {
        $interviewModel = new Interview();
        try {
            $postData = $request->getPost();

            $checkResult = $interviewModel->checkToken($postData);
            if ($checkResult != 1) {
                $interviewModel->sendResult($checkResult);
            }

            $jobseekerid_out = $postData['jobseekerid'];
            $interviewerid_out = $postData['interviewerid'];
            $summary = $postData['summary'];
            $score = $postData['score'];
            $comment = $postData['comment'];

            //$jobseekerid_out = '5dab5588-f349-4e2d-8716-fce67c2be365';
            //$interviewerid_out = '3f256aed-952f-4b15-95d1-2a6259e3e3c9';
            //$summary = '很好';
            //$score = '80';
            //$comment = '录取';

            $interviewModel->updateJobseekerScoreInfo($jobseekerid_out, $interviewerid_out, $summary, $score, $comment);
            $interviewModel->sendResult(1);
        } catch (Exception $ex) {
            $interviewModel->sendResult(-9, $ex->getMessage());
        }
    }


    /**
     * 求职者面试视频录像回看回调
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function jobseekerVideo(Request $request) {
        $interviewModel = new Interview();
        try {
            $postData = $request->getPost();
            $checkResult = $interviewModel->checkToken($postData);
            if ($checkResult != 1) {
                $interviewModel->sendResult($checkResult);
            }

            if (!isset($postData['jobseekerid'])) {
                $interviewModel->sendResult(1);
                return;
            }

            $jobseekerid_out = $postData['jobseekerid'];
            if (empty($jobseekerid_out)) {
                $interviewModel->sendResult(1);
                return;
            }

            //$interviewerid_out = $postData['interviewerid'];
            $videoData = $postData['videodata'];

            //$jobseekerid_out = '5dab5588-f349-4e2d-8716-fce67c2be365';
            //$interviewerid_out = '3f256aed-952f-4b15-95d1-2a6259e3e3c9';
            //$videoData = '';

            if (empty($videoData)) {
                $interviewModel->sendResult(-9, "视频回看地址无内容");
                return;
            }
            $videoData = json_decode($videoData, true);

            if (empty($videoData) || !isset($videoData[0]["fileid"])) {
                $interviewModel->sendResult(-9, "视频回看地址无内容");
                return;
            }

            $result = $interviewModel->updateJobseekerVideoInfo($jobseekerid_out, $videoData);
            if (!$result) {
                $interviewModel->sendResult(-9, '数据保存失败');
                return;
            }
            $interviewModel->sendResult(1);
        } catch (Exception $ex) {
            $interviewModel->sendResult(-9, $ex->getMessage());
        }
    }

    /**
     * 求职者获取面试详情
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws Exception
     */
    public function interviewDetail(Request $request) {
        $jobSeekerIdOut = $request->getParam('jobseekerid_out', 0);
        $interviewModel = new Interview();
        $detailInfo = $interviewModel->getInterviewDetail($jobSeekerIdOut);
        return $this->json(['status' => true, 'message' => '获取成功', 'info' => $detailInfo ?? []]);
    }
}